import nyNetwork from "@/api/nyBaseNetwork";
import DZModelQuestion from "../model/DZModelQuestion";
import DZModelRule from "../model/DZModelRule";
import DZModelRuleResult from "../model/DZModelRuleResult";

class DZServiceRule {

    assess = "";

    key_rule = "";

    list_rule: Array<DZModelRule> = [];

    list_question: Array<DZModelQuestion> = [];

    list_result: Array<DZModelRuleResult> = [];

    m_rule = new DZModelRule();

    m_result = new DZModelRuleResult();

    net_list_rule() {
        var list_data = [];
        nyNetwork.net("规则列表", { assess: this.assess }, data => {
            for (const item of data) {
                var rule = new DZModelRule();
                rule.id = item.iId;
                rule.name = item.sName;
                rule.describe = item.sDescribe;
                if (item.sListQuestion) {
                    var questionList = (item.sListQuestion as string).split(",");
                    for (const question of questionList) {
                        rule.list_question.push(parseInt(question));
                    }
                }
                if (item.sAboutRule) {
                    var aboutRuleList = (item.sAboutRule as string).split(",");
                    for (const aboutRule of aboutRuleList) {
                        rule.list_about_role.push(parseInt(aboutRule));
                    }
                }
                rule.compute_mode = parseInt(item.iComputeMode);
                rule.base = item.iBase;
                rule.result_type = item.iResultType;
                rule.percentage_base = item.iPercentageBase;
                rule.sort = item.iSort;
                list_data.push(rule);
            }
            this.list_rule = list_data;
            console.log(this.list_rule)
        })
    }
    net_list_question(assess) {
        var list_data = [];
        nyNetwork.net("问题字典列表", { assess: assess }, data => {
            for (const item of data) {
                var question = new DZModelQuestion();
                question.id = item.iId;
                question.info = item.sInfo;
                list_data.push(question);
            }
            this.list_question = list_data;
        })
    }
    net_add_rule(success: () => void) {
        var params = {
            assess: this.assess,
            mode: this.m_rule.compute_mode,
            base: this.m_rule.base,
            name: this.m_rule.name,
            describe: this.m_rule.describe,
            question: this.m_rule.list_question_str,
            aboutRule: this.m_rule.list_about_role_str,
            resultType: this.m_rule.result_type,
            percentage: this.m_rule.percentage_base,
            sort: this.m_rule.sort,
        }
        nyNetwork.net("添加规则", params, data => {
            success();
        })
    }

    net_edit_rule(success: () => void) {
        var params = {
            id: this.m_rule.id,
            mode: this.m_rule.compute_mode,
            base: this.m_rule.base,
            name: this.m_rule.name,
            describe: this.m_rule.describe,
            question: this.m_rule.list_question_str,
            aboutRule: this.m_rule.list_about_role_str,
            resultType: this.m_rule.result_type,
            percentage: this.m_rule.percentage_base,
            sort: this.m_rule.sort,
        }
        nyNetwork.net("修改规则", params, data => {
            success();
        })
    }

    net_list_rule_result() {
        var list_data = [];
        nyNetwork.net("结果规则列表", { rule: this.m_rule.id }, data => {
            for (const item of data) {
                var result = new DZModelRuleResult();
                result.id = item.iId;
                result.min = item.iMinScore;
                result.max = item.iMaxScore;
                result.name = item.sName;
                result.result = item.sResult;
                result.proposal = item.sProposal;
                result.resultType = item.iResultType;
                result.minNum = item.iMinNum;
                result.maxNum = item.iMaxNum;
                list_data.push(result);
            }
            this.list_result = list_data;
        })
    }

    net_add_rule_result(success: () => void) {
        var params = {
            rule: this.m_rule.id,
            ...this.m_result
        }
        nyNetwork.net("添加结果规则", params, data => {
            success();
        })
    }

    net_edit_rule_result(success: () => void) {
        var params = {
            ...this.m_result
        }
        nyNetwork.net("编辑结果规则", params, data => {
            success();
        })
    }

    net_del_rule_result(id, success: () => void) {
        nyNetwork.net("删除结果规则", { id: id }, data => {
            success();
        })
    }

}
export default DZServiceRule;